package com.supervision.manage.service

import com.supervision.domain.bean.dc.SdkApp
import com.tencentcloudapi.common.Credential
import com.tencentcloudapi.common.profile.ClientProfile
import com.tencentcloudapi.common.profile.HttpProfile
import com.tencentcloudapi.ocr.v20181119.OcrClient
import com.tencentcloudapi.sms.v20190711.SmsClient
import org.springframework.stereotype.Service

@Service
class TencentSdkService {

    companion object {
        const val REGION = "ap-shanghai"
    }

    var credential: Credential? = null

    private fun initCredential(): Credential {
        if (credential == null) {
            val sdkApp = SdkApp.initSdkApp(1)
            credential = Credential(sdkApp.secretId, sdkApp.secretKey)
        }
        return credential as Credential
    }

    private fun clientProfile(endPoint: String): ClientProfile {
        val httpProfile = HttpProfile()
        httpProfile.endpoint = endPoint
        val clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        return clientProfile
    }

    fun initOcrClient(): OcrClient {
        val cred = this.initCredential()
        val clientProfile = this.clientProfile("ocr.tencentcloudapi.com")

        return OcrClient(cred, REGION, clientProfile)
    }

    fun initSmsClient(): SmsClient {
        val cred = this.initCredential()
        val clientProfile = this.clientProfile("sms.tencentcloudapi.com")

        return SmsClient(cred, REGION, clientProfile)
    }
}